﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Bookvar.UI.Converters
{
    public class IndexToBrushConverter : BookvarConverter<IndexToBrushConverter>, IValueConverter
    {
        #region IValueConverter Members

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            CloseableTabItem item = parameter as CloseableTabItem;
            if ( item == null )
            {
                return null;
            }
            CloseableTabControl tabControl = ItemsControl.ItemsControlFromItemContainer( item ) as CloseableTabControl;
            if ( tabControl == null )
            {
                return null;
            }
            int index = tabControl.ItemContainerGenerator.IndexFromContainer( item );
            //Assembly.GetExecutingAssembly().GetManifestResource

            Brush greenTabBrush =
                tabControl.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "GreenTabBrush" ) ) as
                Brush;
            Brush yellowTabBrush =
                tabControl.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "YellowTabBrush" ) ) as
                Brush;
            Brush blueTabBrush =
                tabControl.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "BlueTabBrush" ) ) as
                Brush;
            Brush purpleTabBrush =
                tabControl.FindResource( new ComponentResourceKey( typeof ( CloseableTabItem ), "PurpleTabBrush" ) ) as
                Brush;

            switch ( index % 4 )
            {
                case 0 :
                {
                    return greenTabBrush;
                }

                case 1 :
                {
                    return yellowTabBrush;
                }

                case 2 :
                {
                    return blueTabBrush;
                }
                case 3 :
                {
                    return purpleTabBrush;
                }
            }
            return null;
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException( "The method or operation is not implemented." );
        }

        #endregion
    }
}